package com.lzh.others.basejava.Thread_21.bilibili_course_thread;

import com.lzh.others.basejava.Thread_21.utils.SmallTool;

import java.util.concurrent.CompletableFuture;

/**
 * @Description: 场景5：
 * 小白等车，700路和800路都能回家，那一趟先来坐哪一趟
 * applyToEither: 上个任务和本任务一起运行，先完成的把任务结果交给Function
 * @Author: LiuZiHao
 * @Date: 2023/1/3 10:19
 */
public class CompletableFuturePrac_04_2 {
    public static void main(String[] args) {
        SmallTool.printTimeAndThread("小白走出餐厅，来到公交车站");
        SmallTool.printTimeAndThread("等待 700路 或者 800路 公交到来");

        CompletableFuture<String> bus = CompletableFuture.supplyAsync(() -> {
            SmallTool.printTimeAndThread("700路公交正在赶来");
            SmallTool.sleepMillis(600);
            return "700路到了";
        }).applyToEither(CompletableFuture.supplyAsync(() -> {
            SmallTool.printTimeAndThread("800路公交正在赶来");
            SmallTool.sleepMillis(300);
            return "800路到了";
        }), firstComeBus -> firstComeBus);
        SmallTool.printTimeAndThread(String.format("%s,小白坐车回家", bus.join()));
    }
}
